package {
	
	import flash.display.*;
	import flash.events.*;
	import flash.geom.Point;
	import flash.system.*;
	import flash.text.*;
	import flash.ui.Keyboard;
	import flash.utils.*;
	
	import org.papervision3d.cameras.*;
	import org.papervision3d.core.geom.Lines3D;
	import org.papervision3d.core.geom.renderables.Line3D;
	import org.papervision3d.core.geom.renderables.Vertex3D;
//	import org.papervision3d.core.layers.RenderLayer;
	import org.papervision3d.core.render.data.RenderHitData;
	import org.papervision3d.events.InteractiveScene3DEvent;
	import org.papervision3d.lights.PointLight3D;
	import org.papervision3d.materials.*;
	import org.papervision3d.materials.shaders.GouraudShader;
	import org.papervision3d.materials.shaders.ShadedMaterial;
	import org.papervision3d.materials.special.LineMaterial;
	import org.papervision3d.objects.primitives.*;
	import org.papervision3d.render.*;
	import org.papervision3d.scenes.*;
	import org.papervision3d.view.*;
	
	[SWF(frameRate="31", width="800", height="800", backgroundColor="0x3D403E")]
	public class Drag3D extends Sprite{
		
		private var viewport:Viewport3D;
		private var renderer:BasicRenderEngine = new BasicRenderEngine();
		private var scene:Scene3D = new Scene3D();
		private var camera:Camera3D = new Camera3D();

// ????????????
		public var dragPlane:Plane = new Plane(new ColorMaterial(0x00FF00, 0), 1500,1500, 6, 6);
		
		[Embed(source="meshes/bamboo.jpg")]
		public var bamboo:Class;
		
		public var sphere:Sphere;
		
		private var angC:Number= 0;// ????????????
		private var ang:Number=0;// ????????????
		private var by:Number = 0;// ????????????
		private var autoRotate:Boolean = false;// ????????????
		private var gravity:Boolean = false; // ????????????
		private var locked:Boolean = false;
		
		public var isDragging = false;
		
		private var vx:Number = 0;// ????????????
		private var vy:Number = 0;
		private var vz:Number = 0;
		
		
		// ????????????
		private var ox:Number;
		private var oy:Number;
		private var oz:Number;
		
		
	
		
		public function Drag3D():void {
			
			stage.align = StageAlign.TOP_LEFT;
			stage.quality = StageQuality.MEDIUM;
			
			
			var tFormat:TextFormat = new TextFormat("Arial");
			tFormat.color = 0xFFFFFF;
			tFormat.bold = true;
			
			
			viewport = new Viewport3D(800, 800, false, true);
			addChild(viewport);
			
			var light:PointLight3D = new PointLight3D();
			light.x = -1000;
			light.z = 1000;
			light.y = 1000;
			var gour:GouraudShader = new GouraudShader(light, 0xFFFFFF, 0x404040);
			var bm:BitmapMaterial = new BitmapMaterial(Bitmap(new bamboo()).bitmapData);
			var shadeMat:ShadedMaterial = new ShadedMaterial(bm, gour);
			
			
			sphere = new Sphere(shadeMat, 100);
			
			scene.addChild(sphere);
			scene.addChild(dragPlane);
			camera.zoom = 11;
			camera.x = 1000;
			camera.z = 1000;
			camera.y = 100;
			
			sphere.material.interactive = true;
			
			dragPlane.material.interactive = true;
			dragPlane.rotationX = -90;
			

			sphere.addEventListener(InteractiveScene3DEvent.OBJECT_PRESS, handlePress);
			addEventListener(Event.ENTER_FRAME, enterFrameFunction);
			addEventListener(MouseEvent.MOUSE_UP, handleRelease);
		}
		
		public function handlePress(e:InteractiveScene3DEvent){
		
	
			isDragging  = true;
			sphere.material.interactive = false;
			dragPlane.material.interactive = true;
		}
		
		public function handleRelease(e:MouseEvent){
			
			isDragging  =false;
//			sphere.material.interactive = true;
//			dragPlane.material.interactive = false;
			
//			e.updateAfterEvent();
		}
		
		
		
		private function enterFrameFunction(e:Event):void{
			if(isDragging){
				
				var rh:RenderHitData = viewport.hitTestPoint2D(new Point(viewport.containerSprite.mouseX, viewport.containerSprite.mouseY));
				
				if(rh.hasHit){
					sphere.x = rh.x
					sphere.y = rh.y;
					sphere.z = rh.z;
				}

				
			}			
			
			
//			camera.x = Math.sin(angC)*1200;
//			camera.z = Math.cos(angC)*1200;
//			camera.y = 500;
			
			render();
		}
		
		private function render():void {
			renderer.renderScene(scene, camera, viewport);
		}
				
		
	}
}
